import axios from "axios";
// import errorCode from '@/utils/errorCode'

axios.defaults.headers['Content-Type'] = 'application/json;charset=utf-8'

// 创建axios实例
const service = axios.create({
    // axios中请求配置有baseURL选项，表示请求URL公共部分
    baseURL: process.env.VUE_APP_BASE_API,
    baseURL2: process.env.VUE_APP_BASE_API2,
    // baseURL: process.env.VUE_APP_IFRAME_PREFIX,
    // 超时
    timeout: 30*60*1000
})

service.interceptors.request.use(function (config) {
    const isToken = (config.headers || {}).isToken === false
    if (!isToken) {
      config.headers['Authorization'] = 'Bearer ' + "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJhbGwiLCJ1aWQiOiIxMTA1ODgzNDgxMDExNjU5MTEiLCJhdWQiOiJhZG1pbiIsImlzcyI6ImZlbG9yZC5jbiIsInR5cGUiOiJhY2Nlc3MiLCJleHAiOiIyMDI0LTAxLTI0IDA5OjM5OjExIiwiaWF0IjoiMjAyNC0wMS0yNCAwOTowOToxMSIsImp0aSI6IjI0NWE4ZWFiLWIzNGYtNDE1NS1hZDkzLWE1NDhiNGZjZjgwNyJ9.Gdm2A-J03HHdzVEQBTdA2zwROzhSNZwKmb9lmKXqipwT2yykAjf3LRSWj0k8VBHAwwEqV336qnVgFEBRpwQFhpb6JDLRn84X9xhfuiGUwOZ1PNpNl5gvIqqaNexdtQ__9u5m371DrdE4rg5qwpFHpUm1p0Cr3scAjvjCXhf4HnbT6kFd1jItP__9hao2qpM6McHMZMCBAvMNBsjYQfOb2mPsLzudzu-S-Aow5yFdp2SLRyv9UEe0ITqiYOFlSQkHcE4YMrjYuCK1-OD7YhOdwx6yTclPhrrgLs3tFL7cfhjVoASO1Jfinm4DfcIojp57VGSIUZCtbd0Qqf_fhH3-kw" // 让每个请求携带自定义token 请根据实际情况自行修改
    }
    return config;
  }, function (error) {
    // 对请求错误做些什么
    return Promise.reject(error);
    }
);

// 添加响应拦截器
service.interceptors.response.use(function (res) {
    // 未设置状态码则默认成功状态  失败 code 01 成功00
    // const code = res.data.code || '0';
    console.log('响应拦截器',res);
//     // 获取错误信息
    // const message = res.data.msg || res.data.mag || errorCode[code] || errorCode['default']
    if(res.status !== 200) {
      return Promise.reject('error')
    }

    return res.data
}
)

export default service
